<?php

class tStandard_User extends tStandard_TreeItem {

	public $anonymous = true;
	public $timeout = 3000; //max inactivity time
	public $changeKeyAfter = 300; //key in cookie will be changed often to bring better security
	
	private $keyId = '_oxuserid';
	private $keyKey = '_oxtempkey';
	
	private $isadmin;

	function recognize() 
	{
		if (is_array($_POST['_action']))
		$action=array_pop(array_keys($_POST['_action']));
		
		if ($action=='login') $this->handleLogin($_POST);

		$key = $_COOKIE[$this->keyKey];
		$id = $_COOKIE[$this->keyId];
		if ($key!="") 
		{
			$raw = mysql_fetch_array($this->select("id = $id"));
			if ($raw["key"]==$key && $raw["state"]=="online") {
				$this->id = $raw["id"];
				if ($action=='signout') {
					$this->handleSignout($_POST);
				} else {
					$this->load();
					return;
				}
			}
		}
		$user = $this->cms->tree->getRaw("tags = 'anonymous_user'");

		if ($user[0]["id"]!="") {
			$this->id = $user[0]["id"];
			$this->refresh();
			$this->anonymous = true;
		} else throw new cException("Nenašel jsem anonymního usera.. ten má 'tags' nastaven na 'anonymous_user'");	
	}
	
	function load($logging=false) 
	{
		
		parent::refresh();
		if (($this->visibility)!="published") { throw new cException("User ještě nebyl aktivován"); }
		if (!$this->anonymous && !$logging && ((time() - strtotime($this->data->standard_user->lasthit))>$this->timeout)) {
			$this->disconnect();
		} else {
			$this->anonymous = false;
			
			$this->clipboard = unserialize((string)$this->data->standard_user->clipboard);
			
			//change key after some time
			if (time() - strtotime($this->data->standard_user->lasthit)>$this->changeKeyAfter) {
				$_COOKIE[$this->keyId] = $id = $this->id;
				$_COOKIE[$this->keyKey] = $key = base_convert(rand(999999999,999999999999999),10,30);
				setcookie ($this->keyId, $id,time()+1000);
				setcookie ($this->keyKey, $key,time()+1000);
				$this->update("`lasthit`='".date("Y-m-d H:i:s")."',`key`='$key' ");
			} else $this->update("`lasthit`='".date("Y-m-d H:i:s")."' ");
		}

	}
	
	function disconnect() 
	{
		$this->update("`state`='disconnected',`key`='' ");
		setcookie ($this->keyId, '', 0);
		setcookie ($this->keyKey, '', 0);
		unset($_COOKIE[$this->keyId]);
		unset($_COOKIE[$this->keyKey]);
		$this->anonymous = true;
	}
	
	function hasPermissionFor($actions, cNode $node) 
	{
		//$this->complete($this->cms->view);
		//print_r($this->data);
		
		if ($this->getCustom('isadmin') == 'true' && !$this->anonymous) return true;
		
		$where = '(id='.implode(' OR id=',explode(',',$this->getCustom('roles'))).')';
		$role = $this->cms->tree->getNodes($where);
		foreach ($role as $r) 
		{
			$r->complete();
			
			foreach ($actions as $k=>$a) 
			{
				if ($r->permits($a, $node)) 
				{
					unset($actions[$k]);
					if (count($actions)==0) return true;
				}
			}
		} 
		return false;
	}
	
	function handleSignout($post) 
	{
		$this->id = $post["id"];
		$this->disconnect();
	}
	
	function setClipboard($method, $nodes) 
	{
		$this->clipboard = array($method=>$nodes);
		$this->update("`clipboard`='".serialize($this->clipboard)."'");
	}
	
	function handleLogin($post) 
	{
		$user = $this->cms->tree->getRaw("name = '".$post["lname"]."'");
		$user = mysql_fetch_array($this->select("id = '".$user[0]["id"]."'"));
		
		if ($user["password"]==md5($post["sword"]) && $post["lname"]!="") 
		{
			$this->id = $user["id"];
			$_COOKIE[$this->keyId] = $id = $this->id;
			$_COOKIE[$this->keyKey] = $key = base_convert(rand(999999999,999999999999999),10,30);
			setcookie ($this->keyId, $id,time()+1000);
			setcookie ($this->keyKey, $key,time()+1000);
			$this->update("`lasthit`='".date("Y-m-d H:i:s")."',`loggedsince`='".date("Y-m-d H:i:s")."',`state`='online',`key`='$key' ");
		} else {
			//print_r($_POST);
			
			throw new cException("špatné méno, nebo cheslo");
		}
	}

}


?>